package com.example.demo.controller;


import com.example.demo.domain.AjaxResult;
import com.example.demo.domain.KgFile;
import com.example.demo.service.KgFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@RequestMapping("/kg")

public class KgFileController {

    @Autowired
    private KgFileService kgFileService;

    @GetMapping("/load/{filename}")
    public AjaxResult getUrl(@PathVariable String filename) {
        return kgFileService.getUrl(filename);
    }
    @GetMapping("/video/{filename}")
    public AjaxResult getVideoUrl(@PathVariable String filename) {
        System.out.println("get video loading:");
        return kgFileService.getUrl("videos/"+filename);
    }

    @GetMapping("/homeworkRequest/{filename}")
    public AjaxResult getHomeworkRequest(@PathVariable String filename) {
        System.out.println("get homework loading:");
        return kgFileService.getUrl("homeworkRequest/"+filename);
    }

    @GetMapping("/pdf/{filename}")
    public AjaxResult getPDFUrl(@PathVariable String filename) {
        return kgFileService.getUrl("pdf/"+filename);
    }

    @GetMapping("/ppt/{filename}")
    public AjaxResult getPPTUrl(@PathVariable String filename) {
        return kgFileService.getUrl("ppt/"+filename);
    }

    @GetMapping("/textbook/{filename}")
    public AjaxResult getTextBookUrl(@PathVariable String filename) {
        return kgFileService.getUrl("textbook/"+filename);
    }

    //返回上传文件的临时访问链接
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("files") MultipartFile[] files) {
        return kgFileService.upload(files);
    }

    @PostMapping("/upload/ppt")
    public AjaxResult uploadPPt(@RequestParam("files") MultipartFile[] files) {
        return kgFileService.upload(files);
    }

    @PostMapping("/init")
    public AjaxResult initialize(@RequestParam("files") MultipartFile[] files){
        return kgFileService.initialize(files);
    }
//添加文件到cos
    @PostMapping
    public AjaxResult add(@RequestBody KgFile kgFile) {
        kgFileService.add(kgFile);
        System.out.println(kgFile.getId());
        return AjaxResult.success(kgFile.getId());
    }

    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Integer id) {
        List<KgFile> kgFiles = kgFileService.getById(id);
        return AjaxResult.success(kgFiles);
    }

    @DeleteMapping
    public AjaxResult delete(@RequestParam("id") Integer deptId) {
        System.out.println("根据ID删除部门: " + deptId);
        return AjaxResult.success();
    }

}

